package com.hzbc.faceattendance.handler;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

    public static <T> T getBean(Class<T> requiredType) {
        if (applicationContext == null) {
            // 等待上下文初始化或抛出更有意义的异常
            throw new IllegalStateException("ApplicationContext is not initialized yet");
        }
        return applicationContext.getBean(requiredType);
    }

    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static <T> T getBean(String beanName, Class<T> requiredType) {
        return applicationContext.getBean(beanName, requiredType);
    }


    public static boolean containsBean(String beanName) {
        return applicationContext.containsBean(beanName);
    }
}